package com.she.olympic;

import java.util.ArrayList;
import java.util.List;

import ken.app.bean.GetCameraBabyBean;
import ken.app.bean.GetStoreInfoBean;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ApplicationInfo;
import android.preference.PreferenceManager;
import android.util.Log;

public class Olympic extends Application implements OnSharedPreferenceChangeListener {
	// Log Tag
	private static final String TAG = Olympic.class.getSimpleName();
	// Preference...
	public static final String PUSH_ENABLED_PREF_KEY 	= "pushEnabled";
	private static final String REGISTRATIONID_KEY 		= "RegistrationID"; // if it is not registered, set none...or set registration id...
	private static final String ISPROCESSING_KEY 			= "isProcessing";	// the request intent still on the air...
	public SharedPreferences prefs;
	//
	private boolean mIsDEBUG = false;	
	
	private List<GetCameraBabyBean> getCameraBabyData; //可照集寶寶列表ArrayList
	private List<GetStoreInfoBean> storeInfoData ;//商家列表ArrayList
	
	private int babyCatchId;
	private String gameCode = "";
	private String msgGiftPageGoldcode = "";
	private GetStoreInfoBean storeBean = null;
	
	// Cliff Add
	public enum PageIndex { // 加油樹,選手個人樹,選手個人樹登入,選手個人樹採果,選手個人樹留言金牌密碼
							TREE, TREE_CONTESTANY, TREE_CONTESTANY_LOGIN, TREE_CONTESTANY_GIFT0, TREE_CONTESTANY_GIFT1,
							// 個人樹,個人樹登入,個人樹採果
							PERSONAL, PERSONAL_LOGIN, PERSONAL_GIFT,
							// 照集,照集說明,照集完成登入,召集完成留言
							AR, AR_INFO, AR_LOGIN, AR_MESSAGE,
							// 收集冊,收集冊登入,加油寶寶,優惠果實,優惠好禮,優惠果實內容,
							MEMBER, MEMBER_LOGIN, MEMBER_PAGE0, MEMBER_PAGE1, MEMBER_PAGE2, MEMBER_GIFT,
							MORE, MORE_LOGIN}
	public PageIndex mPageIndex = PageIndex.TREE;	
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		//
		mIsDEBUG = (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
		//
        this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
        this.prefs.registerOnSharedPreferenceChangeListener(this);
        //
    	Editor editor = this.prefs.edit();
    	editor.putBoolean(ISPROCESSING_KEY, false).commit(); // We do nothing for now.		
	}
		
	public boolean IsDebuggable() {
		return mIsDEBUG;
	}

    public boolean isPushEnabled() {
        return this.prefs.getBoolean(PUSH_ENABLED_PREF_KEY, false);
    }
    // Set (enable push setting) value we want & get value for checking...
    /*開啟關閉PUSH*/
    public boolean enablePush(boolean enable) {
        // Set value
    	Editor editor = this.prefs.edit();
        editor.putBoolean(PUSH_ENABLED_PREF_KEY, enable).commit();
        // Get value & return...
        return this.prefs.getBoolean(PUSH_ENABLED_PREF_KEY, false);
    }
    //
    public String getRegistrationID() {
    	return this.prefs.getString(REGISTRATIONID_KEY, "");
    }
    //
    public String setRegistrationID(String id) {
    	// Set value
    	Editor editor = this.prefs.edit();
    	editor.putString(REGISTRATIONID_KEY, id).commit();
    	// Get value & return...
    	return this.prefs.getString(REGISTRATIONID_KEY, "");
    }
    //
    public boolean isProcessing() {
    	return this.prefs.getBoolean(ISPROCESSING_KEY, false);
    }
    // Save record that is is busy on sending registration-request or not...
    public boolean setProcessing (boolean isbusy) {
    	// Set value
    	Editor editor = this.prefs.edit();
    	editor.putBoolean(ISPROCESSING_KEY, isbusy).commit();
    	// Get value & return...
    	return this.prefs.getBoolean(ISPROCESSING_KEY, false);
    }	

	public List<GetCameraBabyBean> getCamerababyData(){
		return getCameraBabyData;
	}
	
	public void setCamerababyData(List<GetCameraBabyBean> data){
		getCameraBabyData = null;
		getCameraBabyData = data;
	}
	
	public List<GetStoreInfoBean> getStoreInfoData(){
		return storeInfoData;
	}
	
	public void setStoreInfoData(List<GetStoreInfoBean> data){
		storeInfoData = null;
		storeInfoData = data;
	}
	
	public GetStoreInfoBean getStoreInfoBean(){
		return storeBean;
	}
	
	public void setStoreInfoBean(GetStoreInfoBean data){
		storeBean = null;
		storeBean = data;
	}
	
	public void setBabyCatchId (int babyid){
		babyCatchId = babyid;
	}
	
	public int getBabyCatchId(){
		return babyCatchId;
	}
	
	public void setGameCode (String gamecode){
		gameCode = gamecode;
	}
	
	public String getGameCode(){
		return gameCode;
	}
	
	public void setGiftPageGoldcode (String goldcode){
		msgGiftPageGoldcode = goldcode;
	}
	
	public String getGiftPageGoldcode(){
		return msgGiftPageGoldcode;
	}
	
	public String getGmail(){
		AccountManager manager = AccountManager.get(getApplicationContext());
		Account[] accounts = manager.getAccountsByType("com.google");
		//
		return accounts[0].name;
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences preference, String key) {
		// TODO Auto-generated method stub
		if (mIsDEBUG) Log.d(TAG, "onSharedPreferenceChanged KEY=" + key);
		if (PUSH_ENABLED_PREF_KEY.equals(key)){
			setProcessing(true);
			if (isPushEnabled()){				
				super.startService(new Intent(C2DMRegService.REGISTER_ACTION));
			}else{
				if (getRegistrationID().equals("") != true)
					super.startService(new Intent(C2DMRegService.UNREGISTER_ACTION));
			}
		}
	}

	@Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
		Log.e("ken", "RELAUNCHAPP");
//		reLaunchApp();
	}
	
	public void reLaunchApp(){
		
		Intent it = new Intent(this, TreeActivity.class);
//		it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pendingIntent =PendingIntent.getActivity(this, 0, it, Intent.FLAG_ACTIVITY_NEW_TASK);
		AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
	    am.set(AlarmManager.ELAPSED_REALTIME, 600, pendingIntent);
		
		android.os.Process.killProcess(android.os.Process.myPid());
	}	
}
